package com.hanhanxiaochong.zhongxiangchuanmei.view.shop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.PayDetailShoppingBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.CommonAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.config.ViewHolder;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GlideImgManager;
import com.hanhanxiaochong.zhongxiangchuanmei.widget.ShoppingcartAmountView;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.util.List;

/**
 * Created by RockPhoenix on 2018/5/27.
 */

public class PayDetailAdapter extends CommonAdapter<PayDetailShoppingBean> {

    private OnMoneyChangeListener listener;

    public PayDetailAdapter(Context context, int layoutId, List<PayDetailShoppingBean> datas) {
        super(context, layoutId, datas);
    }

    @Override
    protected void convert(ViewHolder holder, PayDetailShoppingBean bean, int position) {
        LinearLayout llShopTitle = holder.getView(R.id.ll_shop_title);
        TextView shopName = holder.getView(R.id.tv_shop_name);
        LinearLayout llContainer = holder.getView(R.id.ll_item_container);
        llContainer.removeAllViews();
        for (PayDetailShoppingBean.ListBean listBean : bean.getList()) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.ll_pay_detail_good, llContainer, false);
            ImageView ivGoodPic = view.findViewById(R.id.iv_good_pic);
            ImageView ivSelfSupport = view.findViewById(R.id.iv_self_support);
            TextView tvGoodName = view.findViewById(R.id.tv_good_name);
            TextView tvVipPrice = view.findViewById(R.id.tv_vip_price);
            TextView tvNormalPrice = view.findViewById(R.id.tv_normal_price);
            ShoppingcartAmountView amountView = view.findViewById(R.id.good_amount);
            TextView tvPostage = view.findViewById(R.id.tv_postage);

            GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + listBean.getPic().split(",")[0], R.mipmap.img_error, R.mipmap.img_error, ivGoodPic);
            tvGoodName.setText(listBean.getB_name());
            ivSelfSupport.setVisibility(listBean.getSelf_support().equals("y") ? View.VISIBLE : View.GONE);
            tvVipPrice.setText(String.format(ResourcesUtils.getString(R.string.vip_good_price), listBean.getPrice_vip()));
            tvNormalPrice.setText(String.format(ResourcesUtils.getString(R.string.normal_good_price), listBean.getPrice()));
            amountView.setOnBtnClickListener(new ShoppingcartAmountView.BtnClickListener() {
                @Override
                public void onBtnClickPlus(View view, int amount) {
                    amountView.setNum(++amount);
                    listBean.setO_num(String.valueOf(amount));
                    listener.onMoneyChange();
                }

                @Override
                public void onBtnClickDecrease(View view, int amount) {
                    amountView.setNum(--amount);
                    listBean.setO_num(String.valueOf(amount));
                    listener.onMoneyChange();
                }
            });
            tvPostage.setText(listBean.getShipping().equals("1") ? "包邮" : "到付");

            llContainer.addView(view);
        }


        shopName.setText(bean.getC_name() != null ? bean.getC_name() : "总店");
//        llShopTitle.setVisibility(position > 0 && shopName.getText().toString().equals(mDatas.get(position - 1).getC_name()) ? View.GONE : View.VISIBLE);

    }

    public interface OnMoneyChangeListener {
        void onMoneyChange();
    }

    public void setOnMoneyChangeListener(OnMoneyChangeListener listener) {
        this.listener = listener;
    }

}
